package com.hod.reservgrid.client;

import java.util.Date;
import com.google.code.p.gwtchismes.client.GWTCButton;
import com.google.code.p.gwtchismes.client.GWTCDatePicker;
import com.google.code.p.gwtchismes.client.GWTCIntervalSelector;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.FitLayout;
import com.hod.reservgrid.client.maps.OccupationRect;
import com.hod.reservgrid.client.model.Bed;
import com.hod.reservgrid.client.model.BedRecordset;
import com.hod.reservgrid.client.model.Client;
import com.hod.reservgrid.client.model.ClientFormPanel;
import com.hod.reservgrid.client.model.ClientRecordset;
import com.hod.reservgrid.client.model.GearsDataStore;
import com.hod.reservgrid.client.model.Lodging;
import com.hod.reservgrid.client.model.LodgingRecordset;
import com.hod.reservgrid.client.model.Reservation;
import com.hod.reservgrid.client.model.ReservationFormPanel;
import com.hod.reservgrid.client.model.ReservationRecordset;
import com.hod.reservgrid.client.model.Room;
import com.hod.reservgrid.client.model.RoomRecordset;
import com.objetdirect.tatami.client.gfx.Color;
import com.objetdirect.tatami.client.gfx.GraphicCanvas;
import com.objetdirect.tatami.client.gfx.GraphicObject;
import com.objetdirect.tatami.client.gfx.GraphicObjectListener;
import com.objetdirect.tatami.client.gfx.Text;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HOD_ReservGrid extends Composite implements GraphicObjectListener, ClickListener, ChangeListener, EntryPoint{
	private GraphicCanvas canvas;
	private Color currentStrokeColor = Color.BLACK;
	private Color currentFillColor = Color.WHITE;
	private int currentStrokeSize = 1;

	
	
	private void showGraphicObject(GraphicObject object, int x, int y) {
			object.setStroke(currentStrokeColor, currentStrokeSize);			
			object.setFillColor(currentFillColor);			
			this.canvas.add(object, x, y);
	}
	    
	
	public static int aleatorio(int max,int min){
		return (int)(Math.random()*(max-min))+min;		
    }

	public void onModuleLoad() {
			canvas = new GraphicCanvas();				
			canvas.setPixelSize(1200, 1200);
			canvas.addGraphicObjectListener(this);
	        try {
	            final GWTCIntervalSelector intsel = new GWTCIntervalSelector(1);
	            intsel.setMaxdays(30);
	            Button buttonReservation = new GWTCButton("Reservar", new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						// TODO Auto-generated method stub
						
					}
	            	
	            });
	            Button button = new GWTCButton("Show", new ClickHandler() {
	               public void onClick(ClickEvent arg0) {
	            	   canvas.clear();
	                   Date ini = intsel.getInitDate();
	                   Date fin = intsel.getEndDate();
	                   currentStrokeColor = Color.BLACK;
	                   currentFillColor = Color.WHITE;
	                   int k = 0;
	                   while (ini.compareTo(fin)<=0){	                	   
	                	   ++k;
	                	   showGraphicObject(new Text(GWTCDatePicker.formatDate("dd-MM", ini)+""), 40*k, 30);
	                	   ini = GWTCDatePicker.add(ini, "1d"); 
	                   }
	                   Room[] rooms = RoomRecordset.getRoomsByFloor();	   				   
	   				   int l = 1;
	   				   boolean primero;
	   				   for(Room room:rooms){	   					   
	   					  //listado de camas para la habitacion
	   					   primero = true;
						   Bed[] beds = BedRecordset.getBed(room.getIdRoom());
						   if (beds.length>0){
							   for (Bed bed:beds){
								   //showGraphicObject(new Text(room.getFloor()+room.getNumber()+"."+bed.getNumber()+""), 30, 44*l-6);
								   ini = intsel.getInitDate();
			   					   k = 0;
			   					   Client client = null;
			   					   primero = true;
			   					   while (ini.compareTo(fin)<=0){	                	   
				                	   ++k;
				                	   //hospedaje en la cama x para la fecha ini
				                	   client = LodgingRecordset.existsLodgingforBed(bed.getIdBed(), ini);
				                	   if (client!=null)
				                		   currentFillColor = Color.RED; //ocupada		                			   
				                	   else
				                		   currentFillColor = Color.GREEN; //libre
				                	   if (primero){
				                		  //com.google.gwt.user.client.Window.alert(room.getFloor()+room.getNumber()+"."+bed.getNumber()+"");
				                	      showGraphicObject(new Text(room.getFloor()+room.getNumber()+"."+bed.getNumber()+""), 40*(k-1), 40*l);
				                	      primero = false;
				                	   }
				                	   showGraphicObject(new OccupationRect(30,30,room,bed,ini,currentFillColor,client),40*k,40*l);
				                	   ini = GWTCDatePicker.add(ini, "1d"); 
				                   }
								   ++l;
							   }
						   }
						   else{
							   Client client = null;
							   //showGraphicObject(new Text(room.getFloor()+room.getNumber()), 30, 44*l-6);
							   ini = intsel.getInitDate();
		   					   k = 0;
		   					   while (ini.compareTo(fin)<=0){	                	   
			                	   ++k;
			                	   //hospedaje en la habitacion x para la fecha ini
			                	   client = LodgingRecordset.existsLodgingforRoom(room.getIdRoom(), ini);
			                	   if ((client!=null))
			                		  currentFillColor = Color.RED; //ocupada			                	   
			                	   else   
			                		  currentFillColor = Color.GREEN; //libre
			                	   if (primero){
			                		   showGraphicObject(new Text(room.getFloor()+room.getNumber()), 40*(k-1), 40*l);
			                		   primero = false;
			                	   }			                		   
			                	   showGraphicObject(new OccupationRect(30,30,room,ini,currentFillColor,client),40*k,40*l);
			                	   ini = GWTCDatePicker.add(ini, "1d"); 
			                   }
							   ++l;
						   }
	   				   }
	               }
	            });
	            //RootPanel.get().add(new HTML("<BR/>"));
	            RootPanel.get().add(intsel);
	            RootPanel.get().add(button);
	            //RootPanel.get().add(buttonReservation);
	            RootPanel.get().add(canvas);
				
	            
			} catch (Exception e) {
				// TODO Auto-generated catch block
				com.google.gwt.user.client.Window.alert(e.getMessage());
			}
	}

	@Override
	public void onClick(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onChange(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDblClicked(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		if (graphicObject instanceof OccupationRect){
			GearsDataStore client_ds = GWT.create(Client.class);
			ClientFormPanel cf = new ClientFormPanel(client_ds);
			cf.setObject(((OccupationRect) graphicObject).client);
			Window wndRoom = new Window();
			wndRoom.setLayout(new AnchorLayout());
			wndRoom.setClosable(true); 
			wndRoom.setWidth("80%");
			wndRoom.setHeight("50%");
			wndRoom.add(cf);
			wndRoom.show();
		}
		
	}

	@Override
	public void mouseMoved(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseClicked(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		if (graphicObject instanceof OccupationRect){
			OccupationRect ocup = (OccupationRect) graphicObject;
			if (graphicObject.getFillColor().equals(Color.GREEN)){
			   graphicObject.setFillColor(Color.YELLOW);
			   Client client = new Client();
			   client.setIdClient(null);
			   if (ocup.bed!=null){
	     		   client.setIdRoom(ocup.bed.getIdRoom());
	     		   client.setIdBed(ocup.bed.getIdBed());
			   }else{
				   client.setIdRoom(ocup.room.getIdRoom());
	     		   client.setIdBed(0);
			   }
			   client.setReservationDate(ocup.date);
			   client.setNumberOfNights(1);
			   GearsDataStore client_ds = GWT.create(Client.class);
			   ClientFormPanel cf = new ClientFormPanel(client_ds);
			   cf.setObject(client);
			   Window wndRoom = new Window();  
			   wndRoom.setClosable(true); 
			   wndRoom.setLayout(new FitLayout());  
			   wndRoom.add(cf);
			   wndRoom.show();
			}else{
				//borrar la fecha de reserva del cliente asociado al par room/bed, fecha
				//graphicObject.setFillColor(Color.GREEN);
			}
		}
		
	}
}
